package mydessertstore.web.servlet;

import mydessertstore.domain.Cart;
import mydessertstore.domain.CartItem;
import mydessertstore.domain.Item;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Iterator;

public class RemoveCartItemServlet extends HttpServlet {

    private static final String ERROR_FORM = "/WEB-INF/jsp/common/error.jsp";
    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        String workingItemId = req.getParameter("workingItemId");

        Item item = cart.removeItemById(workingItemId);

        if(item==null){
            session.setAttribute("errorMsg","进行移除的购物车项目为空");
            req.getRequestDispatcher(ERROR_FORM).forward(req,resp);
        }else{
            req.getRequestDispatcher(CART_FORM).forward(req,resp);
        }


    }
}
